import request from '@/utils/request'

//注册接口
export const userRegisterService = (from) => {
  // console.log(username, password, repassword);
  console.log('发送请求')
  console.log(from)
  //修改请求头信息表示参数是一个json格式的
  request.defaults.headers.post['Content-Type'] = 'application/json'
  return request({
    method: 'POST',
    url: '/user/register',
    data: {
      name: from.name,
      phone: from.phone,
      userName: from.userName,
      // password: from.password,
      // rePassword: from.repassword,
      schoolCode: from.schoolCode,
    },
  })
}
//登陆接口
export const userLoginService = ({ username, password, school }) => {
  // 修改请求头信息表示参数是一个json格式的
  request.defaults.headers.post['Content-Type'] = 'application/json'
  return request({
    method: 'POST',
    url: '/user/login',
    data: {
      userName: username,
      password,
      schoolCode: school,
    },
  })
}

// 更新用户密码
export const userUpdatePasswordService = (id, pwd) => {
  return request({
    method: 'POST',
    url: '/user/uppassword',
    data: {
      id: id,
      newPassword: pwd,
    },
  })
}

//修改用户信息
export const reviseUserService = (info) => {
  console.log('test======', info)
  return request({
    method: 'POST',
    url: '/user/update/student',
    data: {
      id: info.id,
      openid: info.openId,
      nickname: info.nickname,
      schoolCode: info.schoolcode,
      majorCode: info.majorcode,
      gradeCode: info.gradecode,
      role: info.role,
      personality: info.personality,
      preference: info.preference,
      email: info.email,
      phone: info.phone,
      introduction: info.introduction,
    },
  })
}

//删除用户信息
export const delUserService = (openId) => {
  return request({
    method: 'POST',
    url: '/user/delectStudent',
    data: {
      openId: openId,
      text: '由后台管理员删除',
    },
  })
}

/**
 *
 * @description 查询学生列表
 * @param {number} param0.pageNum - 当前页码
 * @param {number} param0.pageSize - 每页显示的记录数
 * @param {number} [param0.schoolId] - 学校ID（可选）
 * @param {string} [param0.orientation] - 学生方向（可选）
 * @param {number} [param0.majorId] - 专业ID（可选）
 * @returns {Promise}
 */
export const selectUsersService = ({
  pageNum,
  pageSize,
  schoolId,
  orientation,
  majorId,
}) => {
  // 构造请求体，只包含非 undefined 的参数
  const data = {
    pageNum,
    pageSize,
    ...(schoolId !== undefined && { schoolId }), // 仅当 schoolId 非 undefined 时添加
    ...(orientation !== undefined && { orientation }), // 仅当 orientation 非 undefined 时添加
    ...(majorId !== undefined && { majorId }),
  }

  return request({
    method: 'POST',
    url: '/user/all',
    data,
  })
}

export function fetchMajorListService(params) {
  return request.post('/user/majorlist', { majorName: params })
}

export async function fetchMajorList(query = '') {
  const res = await fetchMajorListService(query)
  return res.data || []
}
